import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { AppModule } from '@src/app.module';
import { ValidationPipe } from '@nestjs/common';
import { AllExceptionsFilter } from '@src/common/filter/all-exceptions.filter';
import { TransformInterceptor } from '@src/common/interceptor/transform.interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const httpAdapterHost = app.get(HttpAdapterHost);

  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));

  app.useGlobalFilters(new AllExceptionsFilter(httpAdapterHost));

  app.useGlobalInterceptors(new TransformInterceptor());

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // 只保留白名单字段（推荐）
      forbidNonWhitelisted: true, // 出现多余字段直接报错
      transform: true, // 自动把字面量转成 DTO 实例
      transformOptions: {
        enableImplicitConversion: true, // 允许隐式类型转换
      },
    }),
  );

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
